# 用今天讲的requests实现注册接口的自动化测试
import requests

# 1. 使用params传参
env = "http://192.168.0.103:8088/"
url = env + "futureloan/mvc/api/member/register"

# 用例1：密码小于6位（边界值5）
# cs = {"mobilephone": "18012345678", "pwd": "12345", "regname": "sdfgh"}
# 用例2：密码超过18位（边界值19）
# cs = {"mobilephone": "18012345678", "pwd": "12345678901234567812", "regname": "sdfgh"}
# r = requests.get(url, params=cs)
# print(r.text)
# assert r.json()['status'] == 0
# assert r.json()['code'] == "20108"
# assert r.json()['msg'] == "密码长度必须为6~18"

# 用例3：密码为空
# cs = {"mobilephone": "18012345678", "pwd": "", "regname": "sdfgh"}
# 用例4：手机号码为空
# cs = {"mobilephone": "", "pwd": "123456", "regname": "sdfgh"}
# r = requests.get(url, params=cs)
# print(r.text)
# assert r.json()['status'] == 0
# assert r.json()['code'] == "20103"
# assert r.json()['msg'] == "密码不能为空"
# assert r.json()['msg'] == "手机号不能为空"

# 用例5：手机号码已被注册
# cs = {"mobilephone": "13892791655", "pwd": "123456", "regname": "sdfgh"}
# r = requests.get(url, params=cs)
# print(r.text)
# assert r.json()['status'] == 0
# assert r.json()['code'] == "20110"
# assert r.json()['msg'] == "手机号码已被注册"

# 用例6：手机号注册成功
# cs = {"mobilephone": "18710944088", "pwd": "12345645", "regname": "sdfgh"}
# 用例7：密码注册成功
# cs = {"mobilephone": "1389279165523", "pwd": "123456", "regname": "sdfgh"}
# 用例8：用户名注册成功
# cs = {"mobilephone": "1389279165523", "pwd": "123456", "regname": "sdfgh"}
# r = requests.get(url, params=cs)
# print(r.text)
# assert r.json()['status'] == 1
# assert r.json()['code'] == "10001"
# assert r.json()['msg'] == "注册成功"

# 用例9：手机号码大于11位（边界值12）
# cs = {"mobilephone": "18000000000", "pwd": "123456", "regname": "sdfgh"}
# r = requests.get(url, params=cs)
# print(r.text)
# assert r.json()['status'] == 0
# assert r.json()['code'] == "20109"
# assert r.json()['msg'] == "手机号码格式不正确"

# 用例10 手机号码格式不正确
count = 0
for i in range(0, 10):
    for j in range(0, 10):
        for k in range(0, 10):
            try:
                count += 1
                cs = {"mobilephone": "%d%d%d12345678" % (i, j, k), "pwd": "123456", "regname": "柠檬糖"}
                print("%d%d%d12345678" % (i, j, k))
                r = requests.get(url, params=cs)
                print(r.text)
                assert r.json()['status'] == 0
                assert r.json()['code'] == "20109"
                assert r.json()['msg'] == "手机号码格式不正确"
                with open(r"C:\ApiAutoTest46\day01\手机号码格式不正确.txt","a",encoding="utf-8") as f:
                    f.write("%d%d%d12345678" % (i, j, k) + '\n')
            except AssertionError as e:
                print("断言失败：%d%d%d12345678" % (i, j, k))
                with open(r"C:\ApiAutoTest46\day01\手机号码格式正确.txt","a",encoding="utf-8") as f:
                    f.write("%d%d%d12345678" % (i, j, k) + '\n')
            except Exception as e:
                print(e)
print(count)

'''
测试发现缺陷：
1.一些合法的手机号注册失败，比如199、191
2.密码可以包含空格、中文字符等，与常识不符。
3.手机号正确，密码正确时，用户名超长，报错“服务器异常”，要么提示错误，要么注册成功
4.手机号错误，密码正确时，提示手机号不正确
5.手机号正确，密码
'''
